package com.jy.modules.cims.termporaryplan.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jy.modules.cims.common.controller.CimsBaseController;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.loan.CloanImagesFileImplPlus;
import com.jy.modules.cims.component.loan.ICLoan;
import com.jy.modules.cims.data.dto.PlcImagesDto;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.restservice.exception.AbaboonException;

/**
 * 临时方案 之上传文件
 * <b>类名称：</b>TermporaryPlanUploadController <br/>
 * <b>类描述：</b><br/>
 * <b>修改人：</b><br/>
 * <b>修改备注：</b><br/>
 * <b>版本信息：</b>v1.0.0<br/>
 */
@Controller
@Scope("prototype")
@RequestMapping("/termporaryPlan")
public class TermporaryPlanUploadController extends CimsBaseController {
	
	@Autowired
	@Qualifier("com.jy.modules.cims.component.loan.CloanImagesFileImplPlus")
	private CloanImagesFileImplPlus loanImagesFileImplPlus;

	@Autowired
	@Qualifier("com.jy.modules.cims.component.loan.CLoanImpl")
	private ICLoan iCLoan;

	/**
     * 通过 Controller 请求 跳转 进入至相关 页面
     */
    @RequestMapping(value = "/prepareExecute/{operate}")
    public ModelAndView execute(@PathVariable("operate") String operate) throws AbaboonException {
        ModelAndView model = new ModelAndView();
        
        if("toQuery".equals(operate)){//跳转至 查询页面temporaryPlanUpload.jsp
        	model.setViewName("cims/temporaryPlan/temporaryPlanUpload");	
        }if("toUploadByContractNo".equals(operate)){
        	model.setViewName("cims/temporaryPlan/tempFileUploadByContractNo");	
        }
        
        return model;
    }
   
    
    /**
     *  上传 临时文件
     * @Title termporaryPlanUpload
     * @Description TODO
     * @param request
     * @param dto
     * @param dataMsg
     * @return 
     * DataMsg 
     * @update 
     * @throws
     */
    @RequestMapping(value = "/termporaryPlanUpload")
	@ResponseBody
    public DataMsg termporaryPlanUpload(HttpServletRequest request, PlcImagesDto dto, @ModelAttribute DataMsg dataMsg) throws Exception{
    	String dealCode = (String) this.getParameterString("DealCode");
    	String contractNo = (String) this.getParameterString("ContractNo");
		dto.setContractNo(contractNo);
		dto.setDealCode(dealCode);
		
		List<PlcImagesDto> list = iCLoan.findPlcImagesByCpcAndDealType(SearchParamUtil.getSearchParam(dto));
		if(CollectionsUtils.isEmpty(list)){
			logger.info("交易流水号：{}，合同号：{}没有需要上传的影像！",dealCode,contractNo);
			//throw new SysAssistException("交易流水号："+dealCode+"，合同号："+contractNo+"没有需要上传的影像！");
			dataMsg.setMsg("数据已经存在！不允许新增");
			return dataMsg;
		}
		loanImagesFileImplPlus.doImgByPlcImages(list.get(0));
        dataMsg.setMsg("上传成功,请稍后查询");
        return dataMsg;
    }
    
    @RequestMapping(value = "/tempUploadByContractNo")
	@ResponseBody
    public DataMsg tempUploadByContractNo(HttpServletRequest request, @ModelAttribute DataMsg dataMsg) throws Exception{
    	
   	 	StringBuffer msg = new StringBuffer();
    	String txtAre = request.getParameter("txtAre");
    	if(!SimpleStringUtils.hasText(txtAre)){
    		dataMsg.setMsg("参数为空！");
    		dataMsg.setStatus("NO");
    		return dataMsg;
    	}
    	txtAre = txtAre.replaceAll("，", ",");
    	String tArray [] = txtAre.split(",");
   	 	PlcImagesDto dto = null;
		int errorCnt = 0;
    	for(int i=0;i<tArray.length;i++){
    		String contractNo = tArray[i].trim();
    		try {
	    		if(SimpleStringUtils.hasText(contractNo)){
	    			dto = new PlcImagesDto();
	    			dto.setContractNo(contractNo);
	    	   		List<PlcImagesDto> list = iCLoan.findPlcImagesByDealCodeContractNo(SearchParamUtil.getSearchParam(dto));
	    	   		//开始上传
	    	   		if(CollectionsUtils.isNotEmpty(list)){
	    		   		loanImagesFileImplPlus.doImgByPlcImages(list.get(0));
	    	   		}else{
	    				errorCnt +=1;
	    	   			msg.append("合同号："+contractNo+",没有影像资料;");
	    	   		}
	    		}
			} catch (Exception e) {
				e.printStackTrace();
				errorCnt +=1;
				msg.append("合同号："+contractNo+",异常："+e.getMessage()+";");
			}
    	}
    	if(SimpleStringUtils.hasText(msg.toString())){
    		dataMsg.setMsg(msg.toString()+","+errorCnt+"上传异常！"+(tArray.length-errorCnt)+"上传成功！");
    	}else{
    		dataMsg.setMsg("共"+tArray.length+"笔！全部上传成功！");
    	}
        return dataMsg;
    }
    
}